package com.dabaozhan.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理
 * @author houzhong
 */
public class DateUtils {
	
	/** 时间格式(yyyy-MM-dd) */
	public final static String DATE_PATTERN = "yyyy-MM-dd";

	/** 时间格式(yyyy/MM/dd) */
	public final static String DATE_PATTERN_SLASH = "yyyy/MM/dd";
	
	/** 时间格式(yyyy-MM-dd HH:mm:ss) */
	public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	
	/** 时间格式(yyyy年M月dd日 ah:mm:ss) 代码生成器使用 */
	public final static String DATE_TIME_CHN_PATTERN = "yyyy年M月dd日 ah:mm:ss";

	public static String format(Date date) {
		return format(date, DATE_PATTERN);
	}

	public static String format(Date date, String pattern) {
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			return df.format(date);
		}
		return null;
	}

	public static Date getBeginDate(int tomeSection){
		//tomeSection 时间区间 1:全部 2:近7天 3:近1个月 4:近3个月 5:3个月前
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		Date beginDate = null;
		if(2==tomeSection){
			calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 7);
			beginDate = calendar.getTime();
		}else if(3==tomeSection){
			calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 30);
			beginDate = calendar.getTime();
		}else if(4==tomeSection){
			calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 90);
			beginDate = calendar.getTime();
			// tomeSection =1 或者 5
		}else{
			try {
				beginDate = format.parse("2018-01-01 00:00:00");
			} catch (ParseException e) {
			}
		}
		return beginDate;
	}

	public static Date getEndDate(int tomeSection){
		Calendar calendar = Calendar.getInstance();
		Date date = null;
		if(5==tomeSection){
			calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 90);
			date = calendar.getTime();
		}else{
			date = new Date();
		}
		return date;
	}
	
}
